Modification start date
[BattleCats.git] / Assets / EZ Effects / Scripts / ComplexLight.cs
blobcba6cdc79feab007b80d9a6232cc8d0c863247a2
1 using UnityEngine;
2 using System.Collections;
4 namespace EZEffects
6 [AddComponentMenu("EZ Effects/Complex Light")]
7 public class ComplexLight : MonoBehaviour
9 [Header("Fade Properties")]
10 /// <summary>
11 /// The light to use.
12 /// </summary>
13 public Light Light;
14 /// <summary>
15 /// A gradient that represents the change in color of the light.
16 /// </summary>
17 public Gradient ColorGradient;
18 /// <summary>
19 /// How long to fade the light, in seconds.
20 /// </summary>
21 public float FadeTime;
22 /// <summary>
23 /// Should the light automatically fade when enabled? Useful when used with an EffectImpact or EffectMuzzleFlash.
24 /// </summary>
25 public bool FadeOutOnEnable;
27 [Header("Flicker Properties")]
28 /// <summary>
29 /// Should this light flicker?
30 /// </summary>
31 public bool Flicker;
32 /// <summary>
33 /// The range in intensity the light uses while flickering.
34 /// </summary>
35 public Range IntensityRange;
36 /// <summary>
37 /// How quickly the light should flicker.
38 /// </summary>
39 public float FlickerSpeed;
41 float timer = 0;
42 bool fadeIn, off, fading;
43 Vector2 randomValue;
45 void Start()
47 randomValue = Random.insideUnitCircle.normalized;
49 if (Light == null)
50 Light = GetComponentInChildren<Light>();
52 if (Light == null)
54 Debug.LogError("LightComplex " + gameObject.name + " does not have a light assigned!");
55 this.enabled = false;
56 return;
61 void OnEnable()
63 if (!FadeOutOnEnable)
64 return;
66 fadeIn = false;
67 timer = 0;
68 Light.enabled = true;
69 off = false;
71 StartCoroutine(fade(FadeTime));
74 /// <summary>
75 /// Starts a fade in of the light.
76 /// <param name="time">An optional parameter defining how long to fade the light (in seconds).</param>
77 /// </summary>
78 public void FadeIn(float time = -1)
80 fadeIn = true;
81 timer = 0;
83 if (time == -1)
84 time = FadeTime;
86 if (!fading)
87 StartCoroutine(fade(time));
90 /// <summary>
91 /// Starts a fade out of the light.
92 /// <param name="time">An optional parameter defining how long to fade the light (in seconds).</param>
93 /// </summary>
94 public void FadeOut(float time = -1)
96 fadeIn = false;
97 timer = 0;
99 if (time == -1)
100 time = FadeTime;
102 if (!fading)
103 StartCoroutine(fade(time));
106 IEnumerator fade(float fadeTime)
108 fading = true;
110 if (fadeIn)
111 Light.enabled = true;
113 while (timer < fadeTime)
115 timer += Time.deltaTime;
117 if (fadeIn)
118 Light.color = ColorGradient.Evaluate(1 - (timer / fadeTime));
119 else
120 Light.color = ColorGradient.Evaluate(timer / fadeTime);
122 yield return null;
125 if (fadeIn)
126 off = false;
127 else
129 off = true;
130 Light.enabled = false;
133 fading = false;
136 void Update()
138 if (off || !Flicker)
139 return;
141 float noise = Mathf.PerlinNoise(Time.time * randomValue.x * FlickerSpeed, Time.time * randomValue.y * FlickerSpeed);
142 Light.intensity = IntensityRange.Lerp(noise);